package com.douguo.recipe.provider;

import org.apache.http.client.utils.URIUtils;

import com.douguo.recipe.provider.Documents.Caipin;
import com.douguo.recipe.provider.Documents.Caixi;
import com.douguo.recipe.provider.Documents.Calendar;
import com.douguo.recipe.provider.Documents.Favor;
import com.douguo.recipe.provider.Documents.Kouwei;
import com.douguo.recipe.provider.Documents.Nandu;
import com.douguo.recipe.provider.Documents.Recipes;
import com.douguo.recipe.provider.Documents.Steps;
import com.douguo.recipe.provider.Documents.Tese;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.net.Uri;

public class Provider extends ContentProvider {

	private static final String DATABASE_NAME = "douguo.db";
	private static final int DATABSE_VERSION = 2;
	private static final String TAG = "Provider";

	private static final String CAIXI_TABLE_NAME = "caixi";
	private static final String KOUWEI_TABLE_NAME = "kouwei";
	private static final String CAIPIN_TABLE_NAME = "caipin";
	private static final String TESE_TABLE_NAME = "tese";
	private static final String NANDU_TABLE_NAME = "nandu";
	private static final String RECIPES_TABLE_NAME = "caipu";
	private static final String STEPS_TABLE_NAME = "buzou";
	private static final String FAVOR_TABLE_NAME = "shoucang";
	private static final String CALENDAR_TABLE_NAME = "caili";

	private static final int CAIXI = 1;
	private static final int CAIXI_ID = 2;
	private static final int KOUWEI = 3;
	private static final int KOUWEI_ID = 4;
	private static final int CAIPIN = 5;
	private static final int CAIPIN_ID = 6;
	private static final int TESE = 7;
	private static final int TESE_ID = 8;
	private static final int NANDU = 9;
	private static final int NANDU_ID = 10;
	private static final int CAIPU = 11;
	private static final int CAIPU_ID = 12;
	private static final int BUZOU = 13;
	private static final int BUZOU_ID = 14;
	private static final int SHOUCANG = 15;
	private static final int SHOUCANG_ID = 16;
	private static final int CAILI = 17;
	private static final int CAILI_ID = 18;

	private DatabaseHelper mOpenHelper;
	private static final UriMatcher URI_MATCHER;

	static {
		URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
		URI_MATCHER.addURI(Documents.AUTHORITY, "caixi", CAIXI);
		URI_MATCHER.addURI(Documents.AUTHORITY, "caixi/#", CAIXI_ID);
		URI_MATCHER.addURI(Documents.AUTHORITY, "kouwei", KOUWEI);
		URI_MATCHER.addURI(Documents.AUTHORITY, "kouwei/#", KOUWEI_ID);
		URI_MATCHER.addURI(Documents.AUTHORITY, "caipin", CAIPIN);
		URI_MATCHER.addURI(Documents.AUTHORITY, "caipin/#", CAIPIN_ID);
		URI_MATCHER.addURI(Documents.AUTHORITY, "tese", TESE);
		URI_MATCHER.addURI(Documents.AUTHORITY, "tese/#", TESE_ID);
		URI_MATCHER.addURI(Documents.AUTHORITY, "nandu", NANDU);
		URI_MATCHER.addURI(Documents.AUTHORITY, "nandu/#", NANDU_ID);
		URI_MATCHER.addURI(Documents.AUTHORITY, "caipu", CAIPU);
		URI_MATCHER.addURI(Documents.AUTHORITY, "caipu/#", CAIPU_ID);
		URI_MATCHER.addURI(Documents.AUTHORITY, "buzou", BUZOU);
		URI_MATCHER.addURI(Documents.AUTHORITY, "buzou/#", BUZOU_ID);
		URI_MATCHER.addURI(Documents.AUTHORITY, "shoucang", SHOUCANG);
		URI_MATCHER.addURI(Documents.AUTHORITY, "shoucang/#", SHOUCANG_ID);
		URI_MATCHER.addURI(Documents.AUTHORITY, "caili", CAILI);
		URI_MATCHER.addURI(Documents.AUTHORITY, "caili/#", CAILI_ID);
	}

	private static class DatabaseHelper extends SQLiteOpenHelper {

		public DatabaseHelper(Context context, String name, int version) {
			super(context, name, null, version);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			// create table caixi
			db.execSQL("CREATE TABLE IF NOT EXISTS caixi (" + Caixi._ID
					+ " INTEGER PRIMARY KEY AUTOINCREMENT," + Caixi.NAME
					+ " VARCHAR(20) NOT NULL UNIQUE," + Caixi.IMAGE + " TEXT,"
					+ Caixi.ORDER + " INTEGER);");
			db.execSQL("CREATE INDEX IF NOT EXISTS caixi_name_index on caixi(" + Caixi.NAME
					+ ");");

			// create table kouwei
			db.execSQL("CREATE TABLE IF NOT EXISTS kouwei (" + Kouwei._ID
					+ " INTEGER PRIMARY KEY AUTOINCREMENT," + Kouwei.NAME
					+ " VARCHAR(20) NOT NULL UNIQUE," + Kouwei.IMAGE + " TEXT,"
					+ Kouwei.ORDER + " INTEGER);");

			// create table caipin
			db.execSQL("CREATE TABLE IF NOT EXISTS caipin (" + Caipin._ID
					+ " INTEGER PRIMARY KEY AUTOINCREMENT," + Caipin.NAME
					+ " VARCHAR(20) NOT NULL UNIQUE," + Caipin.IMAGE + " TEXT,"
					+ Caipin.ORDER + " INTEGER);");

			// create table tese
			db.execSQL("CREATE TABLE IF NOT EXISTS tese (" + Tese._ID
					+ " INTEGER PRIMARY KEY AUTOINCREMENT," + Tese.NAME
					+ " VARCHAR(20) NOT NULL UNIQUE," + Tese.IMAGE + " TEXT,"
					+ Tese.ORDER + " INTEGER);");

			// create table nandu
			db.execSQL("CREATE TABLE IF NOT EXISTS nandu (" + Nandu._ID
					+ " INTEGER PRIMARY KEY AUTOINCREMENT," + Nandu.NAME
					+ " VARCHAR(20) NOT NULL UNIQUE," + Nandu.IMAGE + " TEXT,"
					+ Nandu.ORDER + " INTEGER);");

			// create table caipu
			db.execSQL("CREATE TABLE IF NOT EXISTS caipu (" + Recipes._ID
					+ " INTEGER PRIMARY KEY AUTOINCREMENT," + Recipes.NAME
					+ " VARCHAR(20) NOT NULL UNIQUE," + Recipes.THUMB
					+ " TEXT," + Recipes.PIC + " TEXT," + Recipes.MAIN
					+ " TEXT," + Recipes.MINOR + " TEXT," + Recipes.DATE
					+ " TIMESTAMP," + Recipes.TIPS + " TEXT,"
					+ Recipes.AUTHOR_ID + " INTEGER," + Recipes.AUTHOR_NAME
					+ " VARCHAR(20)," + Recipes.TIME + " TEXT,"
					+ Recipes.CAIXI_ID + " INTEGER REFERENCES caixi(_id),"
					+ Recipes.KOUWEI_ID + " INTEGER REFERENCES kouwei(_id),"
					+ Recipes.NANDU_ID
					+ " INTEGER INTEGER REFERENCES nandu(_id),"
					+ Recipes.CAIPIN_ID
					+ " INTEGER INTEGER REFERENCES caipin(_id),"
					+ Recipes.TESE_ID
					+ " INTEGER INTEGER REFERENCES tese(_id));");
			db.execSQL("CREATE INDEX IF NOT EXISTS caipu_name_index on caipu(" + Recipes.NAME
					+ ");");
			db.execSQL("CREATE TRIGGER IF NOT EXISTS caipu_img_cleanup DELETE ON caipu"
					+ " BEGIN "
					+ " SELECT _DELETE_FILE("
					+ Recipes.PIC
					+ ");"
					+ " SELECT _DELETE_FILE(" + Recipes.THUMB + ");" + " END");

			// create table buzou
			db.execSQL("CREATE TABLE IF NOT EXISTS buzou (" + Steps._ID
					+ " INTEGER PRIMARY KEY AUTOINCREMENT," + Steps.DESCRIPT
					+ " TEXT," + Steps.PIC + " TEXT," + Steps.RECIPE_ID
					+ " INTEGER," + Steps.NUMBER + " SHORT);");
			db.execSQL("CREATE TRIGGER IF NOT EXISTS buzou_img_cleanup DELETE ON caipu"
					+ " BEGIN "
					+ " SELECT _DELETE_FILE("
					+ Steps.PIC
					+ ");"
					+ " END");

			// create table shoucang
			db.execSQL("CREATE TABLE IF NOT EXISTS shoucang(" + Favor._ID
					+ " INTEGER PRIMARY KEY AUTOINCREMENT," + Favor.RECIPE_ID
					+ " INTEGER," + Favor.DATE + " TIMESTAMP);");

			// create table caili
			db.execSQL("CREATE TABLE IF NOT EXISTS caili(" + Calendar._ID
					+ " INTEGER PRIMARY KEY AUTOINCREMENT,"
					+ Calendar.RECIPE_ID + " INTEGER," + Calendar.YEARMONTH
					+ " TEXT," + Calendar.DATE + " INTEGER);");

		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			
			switch(oldVersion){
			case 1://数据库版本1：caixi,caipin,tese,nandu,kouwei 表都没有order字段
				db.execSQL("DROP TABLE IF EXISTS " + CAIXI_TABLE_NAME);
				db.execSQL("DROP TABLE IF EXISTS " + KOUWEI_TABLE_NAME);
				db.execSQL("DROP TABLE IF EXISTS " + CAIPIN_TABLE_NAME);
				db.execSQL("DROP TABLE IF EXISTS " + TESE_TABLE_NAME);
				db.execSQL("DROP TABLE IF EXISTS " + NANDU_TABLE_NAME);
				onCreate(db);
				break;
			}
//			db.execSQL("DROP TABLE IF EXISTS " + CAIXI_TABLE_NAME);
//			db.execSQL("DROP TABLE IF EXISTS " + KOUWEI_TABLE_NAME);
//			db.execSQL("DROP TABLE IF EXISTS " + CAIPIN_TABLE_NAME);
//			db.execSQL("DROP TABLE IF EXISTS " + TESE_TABLE_NAME);
//			db.execSQL("DROP TABLE IF EXISTS " + NANDU_TABLE_NAME);
//			db.execSQL("DROP TABLE IF EXISTS " + RECIPES_TABLE_NAME);
//			db.execSQL("DROP TABLE IF EXISTS " + STEPS_TABLE_NAME);
//			db.execSQL("DROP TABLE IF EXISTS " + FAVOR_TABLE_NAME);
//			db.execSQL("DROP TABLE IF EXISTS " + CALENDAR_TABLE_NAME);
//			onCreate(db);
		}

	}

	@Override
	public int delete(Uri uri, String where, String[] whereArgs) {
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		String table = null;
		long id;
		switch (URI_MATCHER.match(uri)) {
		case CAIPU_ID:
			table = RECIPES_TABLE_NAME;
			where = Documents.Recipes._ID + "=?";
			id = ContentUris.parseId(uri);
			whereArgs = new String[] { String.valueOf(id) };
			break;
		case SHOUCANG:
			table = FAVOR_TABLE_NAME;
			break;
		case CAILI_ID:
			table = CALENDAR_TABLE_NAME;
			where = Documents.Calendar._ID + "=?";
			id = ContentUris.parseId(uri);
			whereArgs = new String[] { String.valueOf(id) };
			break;
		}

		return db.delete(table, where, whereArgs);
	}

	@Override
	public String getType(Uri uri) {
		switch (URI_MATCHER.match(uri)) {
		case CAIXI:
			return Caixi.CONTENT_TYPE;
		case CAIXI_ID:
			return Caixi.CONTENT_ITEM_TYPE;
		case KOUWEI:
			return Kouwei.CONTENT_TYPE;
		case KOUWEI_ID:
			return Kouwei.CONTENT_ITEM_TYPE;
		case CAIPIN:
			return Caipin.CONTENT_TYPE;
		case CAIPIN_ID:
			return Caipin.CONTENT_ITEM_TYPE;
		case TESE:
			return Tese.CONTENT_TYPE;
		case TESE_ID:
			return Tese.CONTENT_ITEM_TYPE;
		case NANDU:
			return Nandu.CONTENT_TYPE;
		case NANDU_ID:
			return Nandu.CONTENT_ITEM_TYPE;
		case CAIPU:
			return Recipes.CONTENT_TYPE;
		case CAIPU_ID:
			return Recipes.CONTENT_ITEM_TYPE;
		case BUZOU:
			return Steps.CONTENT_TYPE;
		case BUZOU_ID:
			return Steps.CONTENT_ITEM_TYPE;
		case SHOUCANG:
			return Favor.CONTENT_TYPE;
		case SHOUCANG_ID:
			return Favor.CONTENT_ITEM_TYPE;
		case CAILI:
			return Calendar.CONTENT_TYPE;
		case CAILI_ID:
			return Calendar.CONTENT_ITEM_TYPE;
		}

		throw new IllegalStateException("Unkown URI");
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		long row_id = 0;
		Uri newUri = null;
		// 写到这里
		// if(values == null)
		// {
		// values = new ContentValues();
		// }
		switch (URI_MATCHER.match(uri)) {
		case BUZOU:
			row_id = db.insert(STEPS_TABLE_NAME, null, values);
			newUri = ContentUris.withAppendedId(Steps.CONTENT_URI, row_id);
			break;
		case CAILI:
			row_id = db.insert(CALENDAR_TABLE_NAME, null, values);
			newUri = ContentUris.withAppendedId(Calendar.CONTENT_URI, row_id);
			break;
		case CAIPIN:
			row_id = db.insert(CAIPIN_TABLE_NAME, null, values);
			newUri = ContentUris.withAppendedId(Caipin.CONTENT_URI, row_id);
			break;
		case CAIPU:
			row_id = db.insert(RECIPES_TABLE_NAME, null, values);
			newUri = ContentUris.withAppendedId(Recipes.CONTENT_URI, row_id);
			break;
		case KOUWEI:
			row_id = db.insert(KOUWEI_TABLE_NAME, null, values);
			newUri = ContentUris.withAppendedId(Kouwei.CONTENT_URI, row_id);
			break;
		case NANDU:
			row_id = db.insert(NANDU_TABLE_NAME, null, values);
			newUri = ContentUris.withAppendedId(Nandu.CONTENT_URI, row_id);
			break;
		case SHOUCANG:
			row_id = db.insert(FAVOR_TABLE_NAME, null, values);
			newUri = ContentUris.withAppendedId(Favor.CONTENT_URI, row_id);
			break;
		case TESE:
			row_id = db.insert(TESE_TABLE_NAME, null, values);
			newUri = ContentUris.withAppendedId(Tese.CONTENT_URI, row_id);
			break;
		case CAIXI:
			row_id = db.insert(CAIXI_TABLE_NAME, null, values);
			newUri = ContentUris.withAppendedId(Caixi.CONTENT_URI, row_id);
			break;
		}
		if (newUri != null)
			getContext().getContentResolver().notifyChange(newUri, null);
		return newUri;
	}

	@Override
	public boolean onCreate() {
		mOpenHelper = new DatabaseHelper(getContext(), DATABASE_NAME, DATABSE_VERSION);

		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		Cursor cursor = null;
		String table = null;
		switch (URI_MATCHER.match(uri)) {
		case CAIPU:
			table = RECIPES_TABLE_NAME;
			break;
		case CAIPU_ID:
			table = RECIPES_TABLE_NAME;
			selection = Documents.Recipes._ID + "=?";
			selectionArgs = new String[] { String.valueOf(ContentUris
					.parseId(uri)) };
			break;
		case CAIXI_ID:
			table = CAIXI_TABLE_NAME;
			selection = Documents.Caixi._ID + "=?";
			selectionArgs = new String[] { String.valueOf(ContentUris
					.parseId(uri)) };
			break;
		case CAIPIN_ID:
			table = CAIPIN_TABLE_NAME;
			selection = Documents.Caipin._ID + "=?";
			selectionArgs = new String[] { String.valueOf(ContentUris
					.parseId(uri)) };
			break;
		case KOUWEI_ID:
			table = KOUWEI_TABLE_NAME;
			selection = Documents.Kouwei._ID + "=?";
			selectionArgs = new String[] { String.valueOf(ContentUris
					.parseId(uri)) };
			break;
		case TESE_ID:
			table = TESE_TABLE_NAME;
			selection = Documents.Tese._ID + "=?";
			selectionArgs = new String[] { String.valueOf(ContentUris
					.parseId(uri)) };
			break;
		case NANDU_ID:
			table = NANDU_TABLE_NAME;
			selection = Documents.Nandu._ID + "=?";
			selectionArgs = new String[] { String.valueOf(ContentUris
					.parseId(uri)) };
			break;
		case CAIXI:
			table = CAIXI_TABLE_NAME;
			break;
		case CAIPIN:
			table = CAIPIN_TABLE_NAME;
			break;
		case TESE:
			table = TESE_TABLE_NAME;
			break;
		case KOUWEI:
			table = KOUWEI_TABLE_NAME;
			break;
		case NANDU:
			table = NANDU_TABLE_NAME;
			break;
		case BUZOU:
			table = STEPS_TABLE_NAME;
			break;
		case BUZOU_ID:
			table = STEPS_TABLE_NAME;
			selection = Documents.Steps._ID + "=?";
			selectionArgs = new String[] { String.valueOf(ContentUris
					.parseId(uri)) };
			break;
		case SHOUCANG:
			sortOrder = Documents.Favor.DEFAULT_SORT_ORDER;
			table = FAVOR_TABLE_NAME;
			break;
		case CAILI:
			table = CALENDAR_TABLE_NAME;
			break;
		}
		cursor = db.query(table, null, selection, selectionArgs, null, null,
				sortOrder);
		return cursor;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		SQLiteDatabase qb = mOpenHelper.getWritableDatabase();
		String table = null;
		switch (URI_MATCHER.match(uri)) {
		case CAIPU_ID:
			table = RECIPES_TABLE_NAME;
			selection = Recipes._ID + "=?";
			selectionArgs = new String[] { String.valueOf(ContentUris
					.parseId(uri)) };
			break;
		case CAILI:
			table = CALENDAR_TABLE_NAME;
			break;
		}
		return qb.update(table, values, selection, selectionArgs);
	}

}
